|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
var expect = require('expect.js'); |
|
4
|
|
|
var Grid = require('../lib/grid'); |
|
5
|
|
|
var Position = require('../lib/position'); |
|
6
|
|
|
|
|
7
|
|
|
describe('Grid', function() { |
|
8
|
|
|
describe('setItemPosition', function () { |
|
9
|
|
|
var grid = new Grid(new Position({x:5,y:5}, 'N')); |
|
10
|
|
|
it('should add a position to occupation object', function () { |
|
11
|
|
|
var position = new Position({x:1,y:1}, 'N'); |
|
12
|
|
|
grid.setItemPosition(0, position); |
|
13
|
|
|
expect(grid.occupation[0]).to.equal(position); |
|
14
|
|
|
}); |
|
15
|
|
|
it('should update an item’s position in occupation object', function () { |
|
16
|
|
|
var position = new Position({x:2,y:2}, 'S'); |
|
17
|
|
|
grid.setItemPosition(0, position); |
|
18
|
|
|
expect(grid.occupation[0]).to.equal(position); |
|
19
|
|
|
}); |
|
20
|
|
|
}); |
|
21
|
|
|
describe('getCellOccupation', function () { |
|
22
|
|
|
var grid = new Grid(new Position({x:5,y:5}, 'N')), |
|
23
|
|
|
itemPosition = new Position({x:4,y:4}, 'N'); |
|
24
|
|
|
grid.setItemPosition(0, itemPosition); // set a test item |
|
25
|
|
|
it('should return ’id’ of item who match an occupied cell', function () { |
|
26
|
|
|
var position = new Position({x:4,y:4}, 'N'); |
|
27
|
|
|
expect(grid.getCellOccupation(position)).to.equal(0); |
|
28
|
|
|
}); |
|
29
|
|
|
it('should return ’null’ if no item has been found for the given cell', function () { |
|
30
|
|
|
var position = new Position({x:3,y:3}, 'N'); |
|
31
|
|
|
expect(grid.getCellOccupation(position)).to.equal(null); |
|
32
|
|
|
}); |
|
33
|
|
|
it('should throw an error when position is not valid', function () { |
|
34
|
|
|
expect(function() { |
|
35
|
|
|
grid.getCellOccupation(false); |
|
36
|
|
|
}).to.throwException(/Position is not valid/); |
|
37
|
|
|
}); |
|
38
|
|
|
}); |
|
39
|
|
|
describe('isValid', function () { |
|
40
|
|
|
it('should throw an error when X is not valid', function () { |
|
41
|
|
|
expect(function() { |
|
42
|
|
|
new Grid({x:false,y:5}); |
|
43
|
|
|
}).to.throwException(/Coordinates are not valid/); |
|
44
|
|
|
}); |
|
45
|
|
|
it('should throw an error when Y is not valid', function () { |
|
46
|
|
|
expect(function() { |
|
47
|
|
|
new Grid({x:5,y:false}); |
|
48
|
|
|
}).to.throwException(/Coordinates are not valid/); |
|
49
|
|
|
}); |
|
50
|
|
|
}); |
|
51
|
|
|
}); |